import csv
from objs import Student

stus = [
    Student("001",'张三',23,'男'),
    Student("002",'李四',21,'男'),
    Student("003",'王五',23,'女'),
    Student("004",'赵六',24,'男'),
    Student("005",'张三',25,'女'),
    Student("006",'朱琦',22,'男'),
    Student("007",'赵琦',23,'男')
]
#如果要用csv来写对象，需要将对象转换为字典
stu_dicts = [
    {'no':s.no,'name':s.name,'age':s.age,'gender':s.gender} for s in stus
]
with open('stu.csv','w',encoding='utf-8',newline='') as f:
    fname = [s for s in stu_dicts[0].keys()] #列表生成式
    writer = csv.DictWriter(f,fieldnames=fname)
    writer.writeheader()
    writer.writerows(stu_dicts)